package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCatagoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
@Service
public class ContentCategoryServiceImpl implements ContentCatagoryService{

	@Autowired
	private TbContentCategoryMapper mapper;
	@Override
	public List<EasyUITreeNode> getContentCategory(long parentId) {
		//1.注入mapper
		//2.创建example并设置条件
		TbContentCategoryExample example = new TbContentCategoryExample();
		example.createCriteria().andParentIdEqualTo(parentId);
		//3.查询
		List<TbContentCategory> list = mapper.selectByExample(example);
		
		List<EasyUITreeNode> nodes = new ArrayList<EasyUITreeNode>();
		//4.将TbContentCategory转成EasyUITreeNode
		for (TbContentCategory tbContentCategory : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			
			node.setId(tbContentCategory.getId());
			node.setState(tbContentCategory.getIsParent()?"closed":"open");
			node.setText(tbContentCategory.getName());
			
			nodes.add(node);
		}
		//5.返回
		return nodes;
	}
	@Override
	public TaotaoResult addContentCategory(Long parentId, String name) {
		TbContentCategory category = new TbContentCategory();
		category.setCreated(new Date());
		category.setIsParent(false);
		category.setName(name);
		category.setParentId(parentId);
		category.setStatus(1);
		category.setSortOrder(1);
		category.setUpdated(category.getCreated());
		mapper.insertSelective(category);
		
		TbContentCategory parent = mapper.selectByPrimaryKey(parentId);
		if(parent.getIsParent()==false) {
			parent.setIsParent(true);
			mapper.updateByPrimaryKey(parent);
		}
		return  TaotaoResult.ok(category);
	}
	@Override
	public TaotaoResult deleteContentCategory(long id) {
		TbContentCategory category = mapper.selectByPrimaryKey(id);
	
		if(category.getIsParent()) {
			return TaotaoResult.build(500, "不能删除父节点");
		}else {
			mapper.deleteByPrimaryKey(id);
			return TaotaoResult.ok(category);
		}
	}
	@Override
	public TaotaoResult deleteContentCategoryByRec(long id) {
		TbContentCategory category = mapper.selectByPrimaryKey(id);
		TbContentCategoryExample example = new TbContentCategoryExample();
		example.createCriteria().andParentIdEqualTo(id);
		//3.查询
		List<TbContentCategory> list = mapper.selectByExample(example);
		if(null != list) {
			for (TbContentCategory node : list) {
				deleteContentCategoryByRec(node.getId());
			}
		}
		mapper.deleteByPrimaryKey(id);
		/*//判断父节点下面还有没有子节点
		if(null == getContentCategory(parentId)) {
			TbContentCategory parent = mapper.selectByPrimaryKey(parentId);
			parent.setIsParent(false);
			mapper.updateByPrimaryKey(parent);
			}*/
		return TaotaoResult.ok(category);
	}
	

	@Override
	public void updateContentCategory(long id, String text) {
		TbContentCategory category = mapper.selectByPrimaryKey(id);
		category.setName(text);
		mapper.updateByPrimaryKeySelective(category);
		
	}
	
}
